<template>
  <div class="container">
    <div>
      <!-- 搜索框 -->
      <div class="search-box">
        <el-input
          v-model="searchKeyword"
          placeholder="请输入服装关键词搜索"
          class="search-input"
          clearable
        >
        </el-input>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>

      <!-- 服装列表 -->
      <el-row :gutter="20">
        <el-col :span="6" v-for="item in pageList" :key="item.goodId">
          <el-card class="clothes-card" shadow="hover" @click="goToGoodDetail(item.goodId)">
            <div class="clothes-image">
              <el-image :src="item.images" fit="cover"/>
            </div>
            <div class="clothes-info">
              <h4 class="clothes-name">{{ item.goodName }}</h4>
              <p class="clothes-desc">{{ item.description }}</p>
              <div class="clothes-price">
                <span class="price">¥{{ item.price }}</span>
                <span class="unit">/件</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

    </div>
              <!--分页区域-->
      <el-pagination
        class="pagination"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[8]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="sizes,prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="position: absolute ;bottom: 30px; margin-top: 30px; margin-left: 20%;"
        />
  </div>
</template>

<script setup>
import { ref ,onMounted} from 'vue'
import { useRouter } from 'vue-router'
import { searchClothesApi } from '@/api/search'

const searchKeyword = ref('')
const clothesList = ref([])
const pageList = ref([]); // 当前页的数据（用于显示）

// const clothesList = ref([
//   {
//     id: 1,
//     name: '时尚休闲衬衫',
//     description: '2024春季新款 商务休闲修身款',
//     price: 128,
//     image: 'https://via.placeholder.com/300x400'
//   },
//   {
//     id: 2,
//     name: '简约百搭T恤',
//     description: '纯棉面料 多色可选',
//     price: 89,
//     image: 'https://via.placeholder.com/300x400'
//   },
//   {
//     id: 3,
//     name: '牛仔直筒裤',
//     description: '高腰显瘦 经典款式',
//     price: 199,
//     image: 'https://via.placeholder.com/300x400'
//   },
//   {
//     id: 4,
//     name: '春季连衣裙',
//     description: '甜美淑女 气质款',
//     price: 268,
//     image: 'https://via.placeholder.com/300x400'
//   },
//   // ... 更多数据
// ])



// 筛选标签


// 搜索处理

const getClothesList = async () => {
  const res = await searchClothesApi('')
  clothesList.value = res.data
  total.value = clothesList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}
onMounted(() => {
  getClothesList()
})
const handleSearch = async () => {
  const res = await searchClothesApi(searchKeyword.value)
  clothesList.value = res.data
  total.value = clothesList.value.length
  pagePages.value = Math.ceil(total.value / pageSize.value); // 计算总页数
  updatePaginatedList();   // 更新分页数据
}

//分页
const total = ref(0); // 总条数
const pagePages = ref(1); // 总页数
const currentPage = ref(1); // 当前页码
const pageSize = ref(8);  // 每页显示条数

// 更新分页数据（根据 currentPage 和 pageSize）
const updatePaginatedList = () => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  pageList.value = clothesList.value.slice(start, end);  // 切割出当前页的数据
};

// 处理每页条数变化
const handleSizeChange = (newSize) => {
  pageSize.value = newSize; // 更新每页显示的条目数
  updatePaginatedList();  // 更新分页数据
};

// 处理当前页码变化
const handleCurrentChange = (newPage) => {
  currentPage.value = newPage; // 更新当前页码
  updatePaginatedList();  // 更新分页数据
};


// 跳转详情
const router = useRouter()
const goToGoodDetail = (goodId) => {
  const routeData = router.resolve({
    name: 'goodDetail',
    params: {userId: 1, goodId: goodId }
  })
  window.open(routeData.href, '_blank')
}
</script>

<style scoped lang="scss">
.container{
  min-height: 100vh;
}
.search-box {
  max-width: 600px;
  margin: 0 auto 20px;
  display: flex;
  .search-input {
    width: 100%;
  }
  .el-button {
    margin-left: 20px;
    width: 80px;
  }
}

.clothes-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.clothes-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.clothes-image {
  width: 100%;
  height: 300px;
  overflow: hidden;
}

.clothes-image .el-image {
  width: 100%;
  height: 100%;
  transition: all 0.3s;
}

.clothes-image .el-image:hover {
  transform: scale(1.05);
}

.clothes-info {
  padding: 10px;
}

.clothes-name {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.clothes-desc {
  margin: 8px 0;
  font-size: 14px;
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.clothes-price {
  display: flex;
  align-items: baseline;
}

.clothes-price .price {
  font-size: 18px;
  font-weight: bold;
  color: #ff4400;
}

.clothes-price .unit {
  margin-left: 4px;
  font-size: 12px;
  color: #999;
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .el-col {
    width: 25%;
  }
}

@media screen and (max-width: 992px) {
  .el-col {
    width: 33.33%;
  }
}

@media screen and (max-width: 768px) {
  .el-col {
    width: 50%;
  }
}

@media screen and (max-width: 576px) {
  .el-col {
    width: 100%;
  }
}
</style>